package com.autoapitest.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.autoapitest.common.core.domain.entity.ProjectBaseUserEntity;
import com.autoapitest.common.core.domain.entity.SysUser;
import com.autoapitest.common.enums.DeleteStatus;
import com.autoapitest.common.utils.SecurityUtils;
import com.autoapitest.system.domain.ProjectBase;
import com.autoapitest.system.domain.ProjectBaseUser;
import com.autoapitest.system.mapper.ProjectBaseUserMapper;
import com.autoapitest.system.mapper.SysUserMapper;
import com.autoapitest.system.service.IProjectBaseService;
import com.autoapitest.system.service.IProjectBaseUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目管理成员 服务层实现
 *
 * @author yangsen
 */
@Service
public class ProjectBaseUserServiceImpl  extends ServiceImpl<ProjectBaseUserMapper, ProjectBaseUser> implements IProjectBaseUserService {

    @Autowired
    private IProjectBaseService projectBaseService;


    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 项目管理成员新增校验
     *
     * @param projectBaseUserEntity 项目管理成员
     * @return 结果
     */
    @Override
    public Boolean checkProjectUser(ProjectBaseUserEntity projectBaseUserEntity) {
        LambdaQueryWrapper<ProjectBase> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ProjectBase> projectBaseLambdaQueryWrapper = lambdaQueryWrapper
                .eq(ProjectBase::getProjectId, projectBaseUserEntity.getProjectId())
                .eq(ProjectBase::getDelFlag, DeleteStatus.OK.getCode());
        ProjectBase projectBaseServiceOne = projectBaseService.getOne(projectBaseLambdaQueryWrapper);
        if (ObjectUtil.isNotNull(projectBaseServiceOne) && !CollectionUtils.isEmpty(projectBaseUserEntity.getUserIds())){
            List<SysUser> sysUserList = sysUserMapper.selectUserByIds(projectBaseUserEntity.getUserIds());
            if (sysUserList.size()==projectBaseUserEntity.getUserIds().size()){
                return Boolean.TRUE;
            }
        }
        return Boolean.FALSE;
    }


    /**
     * 项目管理成员新增
     *
     * @param projectBaseUserEntity 项目成员管理新增
     * @return 结果
     */
    @Override
    public Boolean insertProjectUsers(ProjectBaseUserEntity projectBaseUserEntity) {
        String projectId = projectBaseUserEntity.getProjectId();
        LambdaQueryWrapper<ProjectBaseUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProjectBaseUser::getProjectId, projectBaseUserEntity.getProjectId())
                .eq(ProjectBaseUser::getDelFlag, DeleteStatus.OK.getCode());
        List<ProjectBaseUser> projectBaseUsers = list(lambdaQueryWrapper);
        List<Long> userIdsByProjectId = projectBaseUsers.stream().map(ProjectBaseUser::getUserId)
                .collect(Collectors.toList());
        //若项目中已存在对应的用户 则无需添加
        List<Long> projectBaseUserEntityUserIds = projectBaseUserEntity.getUserIds();
        projectBaseUserEntityUserIds.removeAll(userIdsByProjectId);
        ArrayList<ProjectBaseUser> projectBaseUserArrayList=new ArrayList<>();
        for (Long projectBaseUserEntityUserId : projectBaseUserEntityUserIds) {
            ProjectBaseUser projectBaseUser=new ProjectBaseUser();
            projectBaseUser.setProjectId(projectId);
            projectBaseUser.setUserId(projectBaseUserEntityUserId);
            projectBaseUser.setCreateBy(SecurityUtils.getUserId().toString());
            projectBaseUser.setCreateTime(new Date());
            projectBaseUserArrayList.add(projectBaseUser);
        }
        return this.saveBatch(projectBaseUserArrayList);
    }



    public void checkProjectIsSave(ProjectBaseUserEntity projectBaseUserEntity){
        LambdaQueryWrapper<ProjectBase> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ProjectBase> projectBaseLambdaQueryWrapper = lambdaQueryWrapper
                .eq(ProjectBase::getProjectId, projectBaseUserEntity.getProjectId())
                .eq(ProjectBase::getDelFlag, DeleteStatus.OK.getCode());
        ProjectBase projectBaseServiceOne = projectBaseService.getOne(projectBaseLambdaQueryWrapper);
        if (ObjectUtil.isNull(projectBaseServiceOne)){
            throw new RuntimeException("项目信息不存在");
        }
    }
    /**
     * 项目管理成员列表
     *
     * @param projectBaseUserEntity 项目管理成员列表
     * @return 结果
     */
    @Override
    public List<ProjectBaseUser> selectProjectUserList(ProjectBaseUserEntity projectBaseUserEntity) {
        this.checkProjectIsSave(projectBaseUserEntity);
        LambdaQueryWrapper<ProjectBaseUser> lambdaQueryWrapperPR=new LambdaQueryWrapper<>();
        lambdaQueryWrapperPR.eq(ProjectBaseUser::getProjectId, projectBaseUserEntity.getProjectId())
                .eq(ProjectBaseUser::getDelFlag, DeleteStatus.OK.getCode());
        return list(lambdaQueryWrapperPR);
    }


    /**
     * 删除项目管理成员列表
     *
     * @param projectBaseUserEntity 删除项目管理成员列表
     * @return 结果
     */
    @Override
    public Boolean listuserdelete(ProjectBaseUserEntity projectBaseUserEntity) {
        this.checkProjectIsSave(projectBaseUserEntity);
        LambdaQueryWrapper<ProjectBaseUser> lambdaQueryWrapperPR=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ProjectBaseUser> projectBaseUserLambdaQueryWrapper = lambdaQueryWrapperPR.eq(ProjectBaseUser::getProjectId, projectBaseUserEntity.getProjectId())
                .in(ProjectBaseUser::getUserId, projectBaseUserEntity.getUserIds())
                .eq(ProjectBaseUser::getDelFlag, DeleteStatus.OK.getCode());
        return this.remove(projectBaseUserLambdaQueryWrapper);
    }
}
